//
//  UtilsMacro.h
//  MVVM+RACTest
//
//  Created by LF on 2017/6/8.
//  Copyright © 2017年 LF. All rights reserved.
//

#ifndef UtilsMacro_h
#define UtilsMacro_h

#import "LFNetworkTool.h"
#import "LFCacheTool.h"
#import "LFReminderTool.h"
#import "LFRefreshHeader.h"
#import "LFRefreshAutoStateFooter.h"
//#import "LFProfileVM.h"
#import "LFNavigationBar.h"


//baseView
#import "LFBaseView.h"
#import "LFBaseButton.h"
#import "LFButton.h"
#import "LFBaseModel.h"
#import "LFBaseViewController.h"

//类别
#import "UIColor+LF.h"
#import "UIImageView+LF.h"
#import "UIView+LF.h"
#import "UIView+Rect.h"
#import "UIView+Borders.h"
#import "UITableViewCell+LF.h"
#import "UIWebView+YWX.h"
#import "YWXGetCurrentVC.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

// 包裹HTML,适应屏幕
// //    resultStr = [NSString stringWithFormat:@"<html>%@<body>%@</body></html>",head,HTML];
#define HTMLString(HTML) \
({ \
    NSString *head=@"<head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"><style> img{max-width: 100%; height:auto !important;} body{ background-color: #ffffff;font-size:large;padding: 0 10px;}</style></head>"; \
    NSString *resultStr; \
    resultStr = [NSString stringWithFormat:@"<!DOCTYPE html> %@ <body> %@ </body> <script type=\"text/javascript\"> \n var objs = document.getElementsByTagName('img'); \n for (var i =0; objs.length; i++) { \n var img=objs[i]; \n var resizeSrc = img.src+'?x-oss-process=image/resize,w_600';\n img.src=resizeSrc; \n} \n </script> \n </html>",head,HTML]; \
    (resultStr); \
})

// window
#define KWINDOW [UIApplication sharedApplication].keyWindow

// 根视图控制器
#define ROOTVC KWINDOW.rootViewController

// 当前导航栏控制器
#define CURRENT_NAV \
({ \
    UITabBarController *rootVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;\
    UINavigationController *currentNav = (UINavigationController *)rootVc.selectedViewController;\
    (currentNav);\
})

// APPDelegate
#define APPDELEGATE (AppDelegate *)[UIApplication sharedApplication].delegate

// 获取URL参数
#define ParaOfIOS_URL(URL) \
({ \
    NSRange range = [URL rangeOfString:@"?"];\
    NSString *propertys = [URL substringFromIndex:(int)(range.location+1)];\
    NSArray *subArray = [propertys componentsSeparatedByString:@"&"];\
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:4];\
    for (int j = 0 ; j < subArray.count; j++) { \
        NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="];\
        [tempDic setObject:dicArray[1] forKey:dicArray[0]];\
    }\
    (tempDic);\
})

// 获取类名
#define GetClassName(URL) \
({\
    NSRange range = [URL rangeOfString:@"?"];\
    NSString *className = [URL substringToIndex:(int)(range.location)];\
    (className);\
})


#define RGBColor(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

#define kScreenW ([[UIScreen mainScreen]bounds].size.width)
#define kScreenH ([[UIScreen mainScreen]bounds].size.height)

#define kNumFrom375(a) (kScreenW/375 * a)
#define kNumFrom667(a) (kScreenH/667 * a)

#define topAlertSuperView  self.navigationController.navigationBar.superview

#define keyBoardHeight ([[UIScreen mainScreen]bounds].size.height)
/*topbar*/
//输入框的高度
static CGFloat const TextViewH = 36.5;
//键盘切换按钮的高度
static CGFloat const emotionBtnH = 36.5;
//键盘切换按钮的宽度
static CGFloat const emotionBtnW = 36.5;

#define bottomBarH 40
#define sendBtnW 60
#define pages 4
//顶部工具条的高度
#define topBarH   TextViewH - 10
//输入框的宽度
#define TextViewW kScreenW - (kScreenW * 55 / 320)

/*keyBoard*/
//键盘变化时间
static CGFloat const keyBoardTipTime = 0.3;
//每一页的按钮数，包括删除按钮
static CGFloat const emojiCount = 21;
//每一行的按钮数
static CGFloat const KrowCount = 7;
//每一页的行数
static CGFloat const rows = 3;
//键盘高度
#define keyBoardH (4 * kScreenW * 0.0875 +(3 + 1) * ((kScreenW - 7 * kScreenW * 0.0875 ) / 8) + 20)
//表情按钮宽高
#define emotionW kScreenW * 0.0875
//表情页的高度（第一键盘高度）
#define pageH (kScreenW - KrowCount * emotionW) / (KrowCount + 1)

// TableView style为group类型时, 隐藏header和footer的高度
#define LFHideTableViewHeaderAndFooterH \
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001;} \
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.001;}

#define TestImgs @[@"http://e.hiphotos.baidu.com/image/pic/item/cc11728b4710b91274e6c636c4fdfc03934522d6.jpg", \
                   @"http://www.sinaimg.cn/qc/photo_auto/photo/73/17/50747317/50747317_950.jpg", \
                   @"http://www.pp3.cn/uploads/201604/20160413004.jpg", \
                   @"http://g.hiphotos.baidu.com/image/pic/item/42a98226cffc1e172492f2384290f603738de98e.jpg"]

//获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

// 是否是iPhone X
#define iphoneX [YWXGetCurrentVC isIphoneX]

// 下标高度  适配x  普通为:0 x:34
#define toolBarHeight [YWXGetCurrentVC toolBarHeightMethod]

// 标签栏高度  适配x  普通为:49 x:49+34
#define tabBarHeight [YWXGetCurrentVC tabBaritemHeight]

// 机型
#define iPhone_model [YWXGetCurrentVC iphoneModel]

// 系统版本
#define iPhone_verson [[UIDevice currentDevice] systemVersion]

// 导航栏高度
#define navHeight [YWXGetCurrentVC navBarHeight]
#define SAFEHEIGHT_BOTTOM (iphoneX ? 34 : 0)

// 状态栏高度
#define statusHeight [YWXGetCurrentVC statusBarHeight]

// rgb颜色转换（16进制->10进制）
#define COLOR_RGB(rgbValue,a) [UIColor colorWithRed:((float)(((rgbValue) & 0xFF0000) >> 16))/255.0 green:((float)(((rgbValue) & 0xFF00)>>8))/255.0 blue: ((float)((rgbValue) & 0xFF))/255.0 alpha:(a)]
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define RANDOM_COLOR [UIColor randomColor]

// 字体
#define SYSTEM_BOLD_FONT(S)  [UIFont boldSystemFontOfSize:S]
#define SYSTEM_FONT(S)       [UIFont systemFontOfSize:S]
#define CUSTOM_BOLDFONT(S)   [UIFont boldSystemFontOfSize:kNumFrom375(S)]
#define CUSTOM_FONT(S)       [UIFont systemFontOfSize:kNumFrom375(S)]

// UserDefaults存取


//#define TASK_KEY_WITH_NUM(NUM) TASK_DIC[[NSString stringWithFormat:@"%zd",NUM]]
//#define TASK_DIC        @{@"1":TASK_KEY_NEW,@"2":TASK_KEY_RETAIL,@"3":TASK_KEY_PIFA,@"4":TASK_KEY_DAKA}

#define TASK_KEY_(OBJ_ID)    [NSString stringWithFormat:@"TASK_KEY_%@",OBJ_ID] // 存储数据

//#define TASK_KEY_NEW    @"TASK_KEY_NEW" // 新手
//#define TASK_KEY_RETAIL @"TASK_KEY_RETAIL" // 零售
//#define TASK_KEY_PIFA   @"TASK_KEY_PIFA" // 批发
//#define TASK_KEY_DAKA   @"TASK_KEY_DAKA" // 5+2打卡
#define TASK_DATE_OPEN  [NSString stringWithFormat:@"TASK_DATE_OPEN_%@",LF_USER_ID] // 开启任务时间,每个用户不同

#define IsFirstOpen(S) [[NSUserDefaults standardUserDefaults]boolForKey:OpenKey(S)]
#define OpenKey(S) [NSString stringWithFormat:@"is_first_open_%@",S]


#define LAST_SIGNED_TIME_KEY [@"LAST_TIME_KEY" stringByAppendingString:LF_USER_INFO.user_id] // 拼接的签到时间key值

#define DELETELISTTHING(KEY) [[NSUserDefaults standardUserDefaults] removeObjectForKey:KEY];
#define SEEKPLISTTHING(KEY)     [[NSUserDefaults standardUserDefaults]objectForKey:KEY]
#define DEPOSITLISTTHING(VALUE,KEY)  \
[[NSUserDefaults standardUserDefaults] setObject:VALUE forKey:KEY];\
[[NSUserDefaults standardUserDefaults] synchronize];

#define REMOVELISTTHING(KEY) [[NSUserDefaults standardUserDefaults] removeObjectForKey:KEY];
#define USERDEFAULTS [NSUserDefaults standardUserDefaults]


// 是否允许通知
#define AllowNotiKey @"AllowNotiKey"
#define dynamicSettingKey @"dynamicSettingKey"

// 判断字段时候为空的情况
#define IF_NULL_TO_STRING(x) ([(x) isEqual:[NSNull null]]||(x)==nil)? @"":TEXT_STRING(x)
// 转换为字符串
#define TEXT_STRING(x) [NSString stringWithFormat:@"%@",x]

// 判断字段时候为空的情况
#define IF_NULL_TO_STRING(x) ([(x) isEqual:[NSNull null]]||(x)==nil)? @"":TEXT_STRING(x)
// 转换为字符串
#define TEXT_STRING(x) [NSString stringWithFormat:@"%@",x]

//首页离线数据缓存的key(UserDefault)
#define Home_Last_DatasKey @"Home_Last_DatasKey"
//首页离线数据
#define Home_Last_Datas [[NSUserDefaults standardUserDefaults] objectForKey:Home_Last_DatasKey]
// 个人信息
#define LF_USER_INFO    [LFProfileVM sharedVM].userInfo
#define LF_USER_ID      LF_USER_INFO.user_id
#define LF_USER_AVATAR  LF_USER_INFO.user_avatar
#define LF_USER_NAME    LF_USER_INFO.name

//存放用户数组
#define BaPing_AD_key   @"BaPing_AD_key"

#define USER_NAME @"USER_NAME"

// TODO 研究一下这个玩意
//#define IS_LOGIN (((NSString *)SEEKPLISTTHING(USER_ID)).length > 0)
//#define YC_USER_ID IF_NULL_TO_STRING(((NSString *)SEEKPLISTTHING(USER_ID)))
//#define YC_USER_PHONE IF_NULL_TO_STRING(((NSString *)SEEKPLISTTHING(USER_PHONE)))
//#define YC_USER_EASEMOB_NAME IF_NULL_TO_STRING(((NSString *)SEEKPLISTTHING(USER_EASEMOB_NAME)))


//定义UIImage对象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
#define ImageNamed(name) [UIImage imageNamed:name]

#define ResignAllResponder [[UIApplication sharedApplication].keyWindow endEditing:YES];

#define KPlaceHolderAvatar ImageNamed(@"ic_default_avatar")
#define PlaceHolderImage ImageNamed(@"ic_placeholder")

//#define ShowPhotoBrowserURLKey  @"ShowPhotoBrowserURLKey"
//#define ShowPhotoBrowserNote  @"ShowPhotoBrowserNote"
//#define ShowPhotoBrowserIndexKey @"ShowPhotoBrowserIndexKey"

// 播放器销毁
#define AVPlayerDeallocKey @"AVPlayerDeallocKey"


// toolBar隐藏和出现的时间
#define ToolBarAnimateTime 0.3f


#endif /* UtilsMacro_h */
// 设置log可以打印出类名,方法名,行数.

#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"[%s:%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif
